/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bef.dtgenerator.core.mgrImpl.mgrActionAssembler;

import com.inspur.edp.bef.bizentity.operation.BizMgrAction;
import com.inspur.edp.bef.component.base.GspComponent;
import com.inspur.edp.bef.component.detailcmpentity.bemgr.BEMgrMethod;
import com.inspur.edp.bef.dtgenerator.api.mgrActionAssemblerGen.CustomMgrActionMethodGenerator;
import com.inspur.edp.bef.dtgenerator.core.BefCoreGenCtx;
import com.inspur.edp.caf.generator.baseInfo.ParameterInfo;
import com.inspur.edp.caf.generator.baseInfo.TypeInfo;
import com.inspur.edp.caf.generator.method.ClassMethodGenerator;
import com.inspur.edp.lcm.metadata.api.entity.GspMetadata;
import org.eclipse.jdt.core.dom.Block;
import org.eclipse.jdt.core.dom.Modifier.ModifierKeyword;
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;

public class CustomMgrActionAssConstructorGenerator extends ClassMethodGenerator {
  private BefCoreGenCtx coreGenCtx;
  private String name;
  private BizMgrAction action;
  private BEMgrMethod method;

  public CustomMgrActionAssConstructorGenerator(
      BefCoreGenCtx coreGenCtx, String name, BizMgrAction action) {
    this.coreGenCtx = coreGenCtx;
    this.name = name;
    this.action = action;
  }

  @Override
  protected boolean buildIsConstructor() {
    return true;
  }

  @Override
  protected Block buildMethodBody() {
    Block block = ast.newBlock();
    SuperConstructorInvocation superConstructorInvocation = ast.newSuperConstructorInvocation();
    superConstructorInvocation.arguments().add(ast.newSimpleName(CustomMgrActionMethodGenerator.mgrContextParName));
    block.statements().add(superConstructorInvocation);
    return block;
  }

  @Override
  protected void doInitialize() {
    super.doInitialize();

    GspMetadata metadata =
        coreGenCtx
            .getBeGenCtx()
            .getMetadataPool()
            .getMetadata(action.getComponentPkgName(), action.getComponentId());

    GspComponent comp =
        (GspComponent)
            ((metadata.getContent() instanceof GspComponent) ? metadata.getContent() : null);
    if (!comp.getComponentType().equals("BEMgrComponent")) {
      throw new RuntimeException("错误的类型");
    }
    Object tempVar = comp.getMethod();
    method = (BEMgrMethod) ((tempVar instanceof BEMgrMethod) ? tempVar : null);
    // var compMetadata =
    // (BEMgrComponent)coreGenCtx.BEGenContext.MetadataPool.getMetadataContent<GspComponentMetadata>(
    //    action.getComponentPkgName(), action.getComponentId()).Content;
    // method = compMetadata.BEMgrMethod;
  }

  @Override
  protected java.util.ArrayList<ModifierKeyword> getAccessModifiers() {
    java.util.ArrayList<ModifierKeyword> result = new java.util.ArrayList<ModifierKeyword>(1);
    result.add(ModifierKeyword.PUBLIC_KEYWORD);
    return result;
  }

  @Override
  protected String getMethodName() {
    return name;
  }

  @Override
  protected java.util.ArrayList<ParameterInfo> getParameterCollection() {
    return CustomMgrActionMethodGenerator.getMethodParameters(method, coreGenCtx.getBeGenCtx());
  }

  //	@Override
  //	protected java.util.ArrayList<SyntaxNodeOrToken>  buildConstructorInitializerParams() { return
  // new java.util.ArrayList<SyntaxNodeOrToken>(1) {
  // SyntaxFactory.Argument(SyntaxFactory.IdentifierName(CustomMgrActionMethodGenerator.mgrContextParName)) }; }
  //
  //
  //	@Override
  //	protected ConstructorInitializerType  buildConstructorInitializerType() { return
  // ConstructorInitializerType.Base; }

  @Override
  protected TypeInfo getReturnType() {
    return null;
  }
}
